package com.yewu;

import com.util.DBConnection;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.data.*;
public class YeWu {
	private String 			post_Num;
	private Connection		conn;
	private DBConnection	DB;
	public HashMap<String,User> 		userInfo1,userInfo2;
	public School school;
	
	public YeWu(String post_Num){
		this.post_Num=post_Num;					//����ҳ��ʵ�ֵ�ҵ���߼�
												//�������ֻ�����ӵı��
		
		userInfo1 = new HashMap<String, User>();
		userInfo2 = new HashMap<String, User>();
	}
	public void ConnectDB(){
		DB	 = new DBConnection();				//��ʼ������
		DB.ConnectDB();							//��������ݿ�
		conn = DB.getConnection();				//��ȡ����
	}
	public void closeDB(){
		conn = null;							//�ر���ݿ�
		DB.closeConnection();					
	}
	
	/**
	 * ������ӱ�Ż�ȡѧУ����Ϣ 
	 */
	public String getSchoolName(){	
		this.school=new School();//ֻҪ���ø÷�������һ������һ��ѧУ����
		getPost();//����ִ��˳������⣬�����Ż�������ȵȡ�������ִ������������Ϊ�˵õ�������ӵ��˻���Ϣ
		User user = this.userInfo1.get("this"); 
		if(user==null){//
			return "δ�ҵ��û���Ϣ";
		}
		int schoolNum = user.schoolNum;
		String sql = "select * from school "
				   + "where school_num = '"+schoolNum+"'";
		ResultSet result	= null; 
		Statement query		= null;
		try{
			query  = this.conn.createStatement(1004,1007);
			result = query.executeQuery(sql);
			if(result.next()==false){						//���һ�����
				return null;								//�쳣���ʣ����ؿ�ֵ
			}					
			this.school.schoolNum	= schoolNum;
			this.school.name		= result.getString("name");
			this.school.type		= result.getString("type");
			this.school.province	= result.getString("province");
			this.school.city		= result.getString("city");
			this.school.address		= result.getString("address");
			return this.school.name;
		}catch(Exception ex){
			ex.printStackTrace();
			System.out.println("YeWu���getSchoolname()�����׳��쳣(������ݿ�����)");
		}
		return "δ�ҵ�ѧУ��Ϣ";

	}
	public int getTheNumberOfPeople(){
		if(this.school==null){
			getSchoolName();//ѧУ��Ϣδ�������ȴ���
		}
		if(this.school.name==null){//��ȡѧУ��Ϣ�����쳣��
			return -1;
		}
		String sql = "select count(*) as numble from user1 "
				   + "where school_num = '"+this.school.schoolNum+"'";
		ResultSet result	= null; 
		Statement query		= null;
		try{
			query  = this.conn.createStatement(1004,1007);
			result = query.executeQuery(sql);
			if(result.next()==false){						//���һ�����
				return -1;									//�쳣���ʣ�����-1
			}
			this.school.numbleOfPeople=result.getInt("numble");
			return this.school.numbleOfPeople;
		}catch(Exception ex){
			ex.printStackTrace();
			System.out.println("YeWu���getTheNumberOfPeople()�����׳��쳣(������ݿ�����)");
		}
		return -1;
	}
	public int getTheNumberOfPost(){
		if(this.school==null){
			getSchoolName();//ѧУ��Ϣδ�������ȴ���
		}
		if(this.school.name==null){//��ȡѧУ��Ϣ�����쳣��
			return -1;
		}
		String sql = "select count(*) as numble from post,user1 "
				   + "where post.account=user1.account and user1.school_num = '"+this.school.schoolNum+"'";
		ResultSet result	= null; 
		Statement query		= null;
		try{
			query  = this.conn.createStatement(1004,1007);
			result = query.executeQuery(sql);
			if(result.next()==false){						//���һ�����
				return -1;									//�쳣���ʣ�����-1
			}
			this.school.numbleOfPost=result.getInt("numble");
			return this.school.numbleOfPost;
		}catch(Exception ex){
			ex.printStackTrace();
			System.out.println("YeWu���getTheNumberOfPost()�����׳��쳣(������ݿ�����)");
		}
		return -1;
	}
	public int getTheNumberOfAttentionRate(){
		if(this.school==null){
			getSchoolName();//ѧУ��Ϣδ�������ȴ���
		}
		if(this.school.name==null){//��ȡѧУ��Ϣ�����쳣��
			return -1;
		}
		String sql = "select count(*) as numble from user_school "
				   + "where school_num = '"+this.school.schoolNum+"'";
		ResultSet result	= null; 
		Statement query		= null;
		try{
			query  = this.conn.createStatement(1004,1007);
			result = query.executeQuery(sql);
			if(result.next()==false){						//���һ�����
				return -1;									//�쳣���ʣ�����-1
			}
			this.school.numbleOfAttentionRate=result.getInt("numble");
			return this.school.numbleOfAttentionRate;
		}catch(Exception ex){
			ex.printStackTrace();
			System.out.println("YeWu���getTheNumberOfPost()�����׳��쳣(������ݿ�����)");
		}
		return -1;
	}
	public String getAddressOfSchool(){
		if(this.school==null){
			getSchoolName();//ѧУ��Ϣδ�������ȴ���
		}
		if(this.school.address==null){//��ȡѧУ��Ϣ�����쳣��
			return "δ����ѧУ��Ϣ";
		}
		String address=this.school.province+this.school.city+this.school.address;
		return address;
	}
	
	/**
	 *���account��ȡ�û���Ϣ 
	 */
	public User getUserInfo(String account){
		
		String sql = "select * from user1 "
				   + "where account = '"+account+"'";
		ResultSet result	= null; 
		Statement query		= null;
		try{
			query  = this.conn.createStatement(1004,1007);
			result = query.executeQuery(sql);
			if(result.next()==false){						//���һ�����
				return null;								//�쳣���ʣ����ؿ�ֵ
			}					
			User user = new User();
			user.nickname = result.getString("nickname");
			user.account  = result.getString("account");
			user.schoolNum= result.getInt("school_num");
			return user;
		}catch(Exception ex){
			ex.printStackTrace();
			System.out.println("YeWu���getUserInfo()�����׳��쳣(������ݿ�����)");
		}
		return null;
	}
	/**
	 * ������ӵ�ID��ȡ���ӵ�����
	 */
	
	
	public Post getPost(){
		String sql = "select * from post "
				   + "where post_num = '"+this.post_Num+"'";
		ResultSet result	= null; 
		Statement query		= null;
		try{
			query  = this.conn.createStatement(1004,1007);
			result = query.executeQuery(sql);
			if(result.next()==false){						//���һ�����
				return null;								//�쳣���ʣ����ؿ�ֵ
			}					
			Post post = new Post();
			post.postNum	= result.getInt("post_num");	//���ӵ���Ϣ
			post.account	= result.getString("account");
			post.content	= result.getString("content");
			post.theme		= result.getString("theme");
			post.time		= result.getString("time");
			post.imageURL	= result.getString("imageurl");
			post.topicNum	= result.getInt("topic_num");
			userInfo1.put("this",getUserInfo(post.account));//����ӵ���ߵ���Ϣר�����ֵΪthis
			return post;
		}catch(Exception ex){
			ex.printStackTrace();
			System.out.println("YeWu���getpost()�����׳��쳣(������ݿ�����)");
		}
		
		return null;								//��ѯ����׳��쳣�����ؿ�ֵ
	}
	
	
	
	/**
	 *������ӱ�Ż�ȡ�����ӵ����л��� 
	 */
	public List<Direct> getDirects(){
		String sql = "select * from direct_post "
				   + "where post_num = '"+this.post_Num+"'"
				   +" order by time";//��������Ų��ҵ�����һ������
		ResultSet result	= null; 
		Statement query		= null;
		List<Direct> directSet = new ArrayList<Direct>();//�������һ������
		try{
			query  = this.conn.createStatement(1004,1007);
			result = query.executeQuery(sql);
			while(result.next()){
				Direct direct 	= new Direct();
				direct.account	= result.getString("account");
				direct.content	= result.getString("content");
				direct.directNum= result.getInt("direct_num");
				direct.imageURL	= result.getString("imageurl");
				direct.postNum	= result.getString("post_num");
				direct.time		= result.getString("time");
				directSet.add(direct);//�������
				userInfo1.put(direct.directNum+"",getUserInfo(direct.account));
				//System.out.println("�ظ��ı����"+direct.directNum+"");
				//System.out.println(getUserInfo(direct.account));
			}					
		}catch(Exception ex){
			ex.printStackTrace();
			System.out.println("YeWu���getDirects()�����׳��쳣(������ݿ�����)");
		}
		
		return directSet;//�����Ƿ������ۣ����һ���?ǰ̨
	}
	
	//��ݱ�Ų�ѯ�������ֱ�����ۣ�����List<Indirect>
	public List<Indirect> getindirectByNum(int num){
		String sql = "select * from indirect_post "
				   + "where reply_num = '"+num+"'"
				   +" order by time";//��ѯ������ֱ�ӻظ����Ϊnum��indirect����direct������
		ResultSet result	= null;
		Statement query		= null;
		List<Indirect> indirectSet = new ArrayList<Indirect>();//����ֱ�����۵Ĵ��list
		try{
			query  = this.conn.createStatement(1004,1007);
			result = query.executeQuery(sql);
			while(result.next()){
				Indirect direct 	= new Indirect();
				direct.account		= result.getString("account");
				direct.content		= result.getString("content");
				direct.indirectNum	= result.getInt("indirect_num");
				direct.replyNum		= result.getInt("reply_num");
				direct.time			= result.getString("time");
				indirectSet.add(direct);//�������
			}					
		}catch(Exception ex){
			ex.printStackTrace();
			System.out.println("YeWu���getindirectByNum(int num)�����׳��쳣(������ݿ�����)");
		}
		
		return indirectSet;//��������ֱ��������
	}
	
	
	//������Ϊindirect_num(���۵��ǡ��ظ���)������������
	public void getIndirect(int indirect_num,List<Indirect> result,int level){
		List<Indirect> indirectList = new ArrayList<Indirect>();
		indirectList = getindirectByNum(indirect_num);//��ȡ���Ϊindirect_num�����۵�����ֱ������
		for(int i=0;i<indirectList.size();i++){
			Indirect indirect =indirectList.get(i);
			indirect.level=level;//�ͽ���������
			userInfo2.put(indirect.indirectNum+"",getUserInfo(indirect.account));
			//System.out.println("���۵ı����"+indirect.indirectNum+"");
			//System.out.println(getUserInfo(indirect.account));
			result.add(indirect);//�ѵ�ǰ�����ۼ��뵽���
			int num=indirectList.get(i).indirectNum;//��ȡ��ǰ���۵ı��
			getIndirect(num,result,level+1);//���?ǰ���۵������ۣ����������һ���
			
		}//ѭ��������Ϊindirect_num��������ȫ������result���
	}
	
	
	public List<Indirect> getIndirects(int direct_num){//����ÿһ��һ����������������ӻ���Ҳ����һ�������������
		
		List<Indirect> indirect = new ArrayList<Indirect>();//�ظ�һ�������reply_num�Ǹ���������Ҫ��������
		indirect = getindirectByNum(-direct_num);//�õ�һ�������ȫ��ֱ������
		List<Indirect> result = new ArrayList<Indirect>();//��Ž��
		//����ÿһ�����ۣ�����������������ķ�ʽ���ҵ����������ۣ�������
		for(int i=0;i<indirect.size();i++){
			Indirect in = indirect.get(i);
			in.level=1;//ֱ�����۶��ǵ�һ���
			userInfo2.put(in.indirectNum+"",getUserInfo(in.account));
			result.add(in);//�ѵ�ǰ�����ۼ��뵽���
			int indirect_num=indirect.get(i).indirectNum;//��ȡ��ǰ���۵ı��
			getIndirect(indirect_num,result,2);//������Ϊindirect_num�����۵�������
			//�õ��Ľ�������һ�����
		}
		
		return result;//����һ�����������������
	}
	
	
	
	public boolean isAttention(String account,int schoolNum){//��ѯ�û��Ƿ��ע�˸�У
		String sql="select * from user_school where school_num = '"+schoolNum+"' and account = '"+account+"' ";
		Connection conn = DB.getConnection();
		Statement sta;
		try {
			sta = conn.createStatement();
			ResultSet result= null; 
			//System.out.println(sql);
			result = sta.executeQuery(sql);
			if(result.next()){//��������Ϊ�Ѿ���ע,������
				
				return true;
			}else{
				
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("YeWu��isAttention()�׳��쳣");
			return false;
		}

	}
	
	public int isZan(String account,int postNum){//��ѯ�����ظ��Ƿ��û���,0:δ���ۣ�1:�ã�-1:��
		String sql="select * from good_or_bad where post_num = '"+postNum+"' and account = '"+account+"' ";
		Connection conn = DB.getConnection();
		Statement sta;
		try {
			sta = conn.createStatement();
			ResultSet result= null; 
			//System.out.println(sql);
			result = sta.executeQuery(sql);
			if(result.next()){//��������Ϊ�Ѿ���ע,������	
				return result.getInt("type");
			}else{
				return 0;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("YeWu��isZan()�׳��쳣");
			return 0;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
